package Test.MQ.ACK;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Test {
    public static void main(String[] args) {
        countAndPrint(new int[]{-1,2,1,-4},1);
        countAndPrint(new int[]{0,0,0},1);
        countAndPrint(new int[]{1,2,3,4,5},1);
        countAndPrint(new int[]{1,2,3,4,5},5);
        countAndPrint(new int[]{-1,2,1,-1},0);
    }
    public static void countAndPrint(int[] nums, int target) {
        int n = nums.length;

        // 存储每个数与 target 的差的绝对值
        List<int[]> diffList = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            diffList.add(new int[]{Math.abs(nums[i] - target), nums[i]});
        }

        // 按差的绝对值排序
        diffList.sort((a, b) -> Integer.compare(a[0], b[0]));

        // 选择前三个不同的数
        int sum = 0;
        for (int i = 0; i < 3; i++) {
            sum += diffList.get(i)[1];
        }


        System.out.println(sum);
    }

}
